3 * Lock manager registration handling.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup LockManager
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Logger\LoggerFactory
;
27 * Class to handle file lock manager registration
29 * @ingroup LockManager
32 class LockManagerGroup
{
33 /** @var LockManagerGroup[] (domain => LockManagerGroup) */
34 protected static $instances = [];
36 protected $domain; // string; domain (usually wiki ID)
38 /** @var array Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...)) */
39 protected $managers = [];
42 * @param string $domain Domain (usually wiki ID)
44 protected function __construct( $domain ) {
45 $this->domain
= $domain;
49 * @param bool|string $domain Domain (usually wiki ID). Default: false.
50 * @return LockManagerGroup
52 public static function singleton( $domain = false ) {
53 $domain = ( $domain === false ) ?
wfWikiID() : $domain;
54 if ( !isset( self
::$instances[$domain] ) ) {
55 self
::$instances[$domain] = new self( $domain );
56 self
::$instances[$domain]->initFromGlobals();
59 return self
::$instances[$domain];
63 * Destroy the singleton instances
65 public static function destroySingletons() {
66 self
::$instances = [];
70 * Register lock managers from the global variables
72 protected function initFromGlobals() {
73 global $wgLockManagers;
75 $this->register( $wgLockManagers );
79 * Register an array of file lock manager configurations
81 * @param array $configs
84 protected function register( array $configs ) {
85 foreach ( $configs as $config ) {
86 $config['domain'] = $this->domain
;
87 if ( !isset( $config['name'] ) ) {
88 throw new Exception( "Cannot register a lock manager with no name." );
90 $name = $config['name'];
91 if ( !isset( $config['class'] ) ) {
92 throw new Exception( "Cannot register lock manager `{$name}` with no class." );
94 $class = $config['class'];
95 unset( $config['class'] ); // lock manager won't need this
96 $this->managers
[$name] = [
105 * Get the lock manager object with a given name
107 * @param string $name
108 * @return LockManager
111 public function get( $name ) {
112 if ( !isset( $this->managers
[$name] ) ) {
113 throw new Exception( "No lock manager defined with the name `$name`." );
115 // Lazy-load the actual lock manager instance
116 if ( !isset( $this->managers
[$name]['instance'] ) ) {
117 $class = $this->managers
[$name]['class'];
118 $config = $this->managers
[$name]['config'];
119 if ( $class === 'DBLockManager' ) {
120 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
121 $lb = $lbFactory->newMainLB( $config['domain'] );
122 $dbw = $lb->getLazyConnectionRef( DB_MASTER
, [], $config['domain'] );
124 $config['dbServers']['localDBMaster'] = $dbw;
125 $config['srvCache'] = ObjectCache
::getLocalServerInstance( 'hash' );
127 $config['logger'] = LoggerFactory
::getInstance( 'LockManager' );
129 $this->managers
[$name]['instance'] = new $class( $config );
132 return $this->managers
[$name]['instance'];
136 * Get the config array for a lock manager object with a given name
138 * @param string $name
142 public function config( $name ) {
143 if ( !isset( $this->managers
[$name] ) ) {
144 throw new Exception( "No lock manager defined with the name `$name`." );
146 $class = $this->managers
[$name]['class'];
148 return [ 'class' => $class ] +
$this->managers
[$name]['config'];
152 * Get the default lock manager configured for the site.
153 * Returns NullLockManager if no lock manager could be found.
155 * @return LockManager
157 public function getDefault() {
158 return isset( $this->managers
['default'] )
159 ?
$this->get( 'default' )
160 : new NullLockManager( [] );
164 * Get the default lock manager configured for the site
165 * or at least some other effective configured lock manager.
166 * Throws an exception if no lock manager could be found.
168 * @return LockManager
171 public function getAny() {
172 return isset( $this->managers
['default'] )
173 ?
$this->get( 'default' )
174 : $this->get( 'fsLockManager' );